home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-02 / pascalt1.zip / CHAP8.TXT < prev    next >
Text File  |  1988-01-15  |  13KB  |  323 lines

  1.  
  2.                 CHAPTER 8 - Scalars, subranges, and sets.
  3.  
  4.  
  5.                               PASCAL SCALARS
  6.  
  7.              A scalar, also called an enumerated type, is a list  of
  8.         values  which a variable of that type may assume.   Look  at
  9.         the  Pascal program ENTYPES for an example of some  scalars.
  10.         The  first  type declaration defines Days as  being  a  type
  11.         which  can take on any one of seven values.   Since,  within
  12.         the var declaration, Day is assigned the type of Days,  then
  13.         Day  is  a  variable  which can  assume  any  one  of  seven
  14.         different  values.  Moreover Day can be assigned  the  value
  15.         Mon, or Tue, etc., which is considerably clearer than  using
  16.         0  to represent Monday, 1 for Tuesday, etc.  This makes  the
  17.         program easier to follow and understand.
  18.  
  19.              Internally,  Pascal does not actually assign the  value
  20.         Mon   to   the  variable  Day,  but  it  uses   an   integer
  21.         representation for each of the names.  This is important  to
  22.         understand because you need to realize that you cannot print
  23.         out  Mon,  Tue,  etc., but can only use  them  for  indexing
  24.         control statements.
  25.  
  26.              The   second  line  of  the  type  definition   defines
  27.         Time_Of_Day  as  another scalar which can have any  of  four
  28.         different  values, namely those listed.  The  variable  Time
  29.         can only be assigned one of four values since it is  defined
  30.         as  the  type  Time_Of_Day.  It should be  clear  that  even
  31.         though  it  can be assigned Morning, it cannot  be  assigned
  32.         Morning_time or any other variant spelling of Morning, since
  33.         it  is  simply another identifier which must have  an  exact
  34.         spelling to be understood by the compiler.
  35.  
  36.              Several  real  variables  are defined to  allow  us  to
  37.         demonstrate the use of the scalar variables.  After  writing
  38.         a  header  in lines 16 through 20, the  real  variables  are
  39.         initialized  to some values that are probably not real  life
  40.         values, but will serve to illustrate the scalar variable.
  41.  
  42.                         A BIG SCALAR VARIABLE LOOP
  43.  
  44.              The  remainder of the program is one large  loop  being
  45.         controlled by the variable Day as it goes through all of its
  46.         values,  one at a time.  Note that the loop could have  gone
  47.         from Tue to Sat or whatever portion of the range desired, it
  48.         does not have to go through all of the values of Day.  Using
  49.         Day as the case variable, the name of one of the days of the
  50.         week  is  written  out each time we  go  through  the  loop.
  51.         Another loop controlled by Time is executed four times, once
  52.         for each value of Time.  The two case  statements within the
  53.         inner loop are used to calculate the total pay rate for each
  54.         time  period and each day.  The data is formatted  carefully
  55.  
  56.  
  57.  
  58.                                  Page 46
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.                 CHAPTER 8 - Scalars, subranges, and sets.
  69.  
  70.  
  71.         to  make a nice looking table of pay rates as a function  of
  72.         Time and Day.
  73.  
  74.              Take  careful  notice  of  the  fact  that  the  scalar
  75.         variables never entered into the calculations, and they were
  76.         not printed out.  They were only used to control the flow of
  77.         logic.  It was much neater than trying to remember that  Mon
  78.         is  represented by a 0, Tue is represented by a 1, etc.   In
  79.         fact, those numbers are used for the internal representation
  80.         of  the scalars but we can relax and let Pascal worry  about
  81.         the internal representation of our scalars.
  82.  
  83.              Compile and run this program and observe the output.
  84.  
  85.                         LETS LOOK AT SOME SUBRANGES
  86.  
  87.              Examine   the  program  SUBRANGE  for  an  example   of
  88.         subranges   and  some  additional  instruction   on   scalar
  89.         variables.   It  may be expedient to define  some  variables
  90.         that  only  cover a part of the full range as defined  in  a
  91.         scalar type.  Notice that Days is declared a scalar type  as
  92.         in  the  last program, and Work is declared a type  with  an
  93.         even more restricted range.  In the var declaration, Day  is
  94.         once  again  defined  as the days of the  week  and  can  be
  95.         assigned  any  of  the days by the  program.   The  variable
  96.         Workday, however, is assigned the type Work, and can only be
  97.         assigned the days Mon through Fri.  If an attempt is made to
  98.         assign  Workday  the  value Sat, a run-time  error  will  be
  99.         generated.   A carefully written program will never  attempt
  100.         that, and it would be an indication that something is  wrong
  101.         with  either  the program or the data.  This is one  of  the
  102.         advantages  of Pascal over older languages and is  a  reason
  103.         for  the  relatively  strong type checking  built  into  the
  104.         language.
  105.  
  106.              Further examination will reveal that Index is  assigned
  107.         the  range of integers from 1 through 12.  During  execution
  108.         of  the program, if an attempt is made to assign  Index  any
  109.         value  outside  of  that range, a run  time  error  will  be
  110.         generated.  Suppose the variable Index was intended to refer
  111.         to your employees, and you have only 12.  If an attempt  was
  112.         made to refer to employee number 27, or employee number  -8,
  113.         there  is  clearly an error somewhere in the  data  and  you
  114.         would  want to stop running the payroll to fix the  problem.
  115.         Pascal would have saved you a lot of grief.
  116.  
  117.                    SOME STATEMENTS WITH ERRORS IN THEM.
  118.  
  119.              In  order to have a program that would compile  without
  120.         errors, and yet show some errors, the section of the program
  121.         in  lines 16 through 27 is not really a part of the  program
  122.  
  123.  
  124.                                  Page 47
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.                 CHAPTER 8 - Scalars, subranges, and sets.
  135.  
  136.  
  137.         since  it  is  within a comment area.  This is  a  trick  to
  138.         remember  when  you are debugging a program,  a  troublesome
  139.         part can be commented out until you are ready to include  it
  140.         in  the rest.  The errors are self explanatory and it  would
  141.         pay  for you to spend enough time to understand each of  the
  142.         errors.
  143.  
  144.              There  are seven assignment statements as  examples  of
  145.         subrange  variable use in lines 29 through 35.  Notice  that
  146.         the variable Day can always be assigned the value of  either
  147.         Workday or Weekend, but the reverse is not true because  Day
  148.         can  assume  values that would be illegal to assign  to  the
  149.         others.
  150.  
  151.                         THREE VERY USEFUL FUNCTIONS
  152.  
  153.              Lines 37 through 42 of the example program  demonstrate
  154.         the  use  of  three  very  important  functions  when  using
  155.         scalars.   The first is the Succ function that  returns  the
  156.         value  of the successor to the scalar used as  an  argument,
  157.         the  next value.  If the argument is the last value,  a  run
  158.         time  error  is generated.  The next function  is  the  Pred
  159.         function that returns the predecessor to the argument of the
  160.         function.   Finally  the  Ord  function  which  returns  the
  161.         ordinal value of the scalar.
  162.  
  163.              All scalars have an internal representation starting at
  164.         0  and increasing by one until the end is reached.   In  our
  165.         example program, Ord(Day) is 5 if Day has been assigned Sat,
  166.         but Ord(Weekend) is 0 if Weekend has been assigned Sat.   As
  167.         you   gain  experience  in  programming  with  scalars   and
  168.         subranges,  you  will realize the value of these  three  new
  169.         functions.
  170.  
  171.              A few more thoughts about subranges are in order before
  172.         we go on to another topic.  A subrange is always defined  by
  173.         two  predefined  constants,  and is  always  defined  in  an
  174.         ascending  order.  A variable defined as a subrange type  is
  175.         actually  a variable defined with a restricted range.   Good
  176.         programming practice would dictate that subranges should  be
  177.         used as often as possible in order to prevent garbage  data.
  178.         There are actually very few variables ever used that  cannot
  179.         be restricted by some amount.  The limits may give a hint at
  180.         what the program is doing and can help in understanding  the
  181.         program  operation.  Subrange types can only be  constructed
  182.         using the simple types, integer, char, byte, or scalar.
  183.  
  184.              Compile  and  run this program even though  it  has  no
  185.         output.  Add some output statements to see what values  some
  186.         of the variables assume.
  187.  
  188.  
  189.  
  190.                                  Page 48
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.                 CHAPTER 8 - Scalars, subranges, and sets.
  201.  
  202.  
  203.                                    SETS
  204.  
  205.              Now  for  a  new topic, sets.   Examining  the  example
  206.         Pascal  program  SETS  will  reveal  some  sets.   A  scalar
  207.         variable  is  defined first, in this case  the  scalar  type
  208.         named  Goodies.   A set is then defined  with  the  reserved
  209.         words  "set  of"  followed  by  a  predefined  scalar  type.
  210.         Several variables are defined as sets of Treat, after  which
  211.         they  can  individually be assigned portions of  the  entire
  212.         set.
  213.  
  214.              Consider  the  variable Ice_Cream_Cone which  has  been
  215.         defined  as a set of type Treat.  This variable is  composed
  216.         of  as many elements of Goodies as we care to assign to  it.
  217.         In the program, we define it as being composed of Ice_Cream,
  218.         and  Cone.  The set Ice_Cream_Cone is therefore composed  of
  219.         two elements, and it has no numerical or alphabetic value as
  220.         most other variables have.
  221.  
  222.              In  lines 21 through 26, you will see 4 more  delicious
  223.         deserts  defined as sets of their components.   Notice  that
  224.         the banana split is first defined as a range of terms,  then
  225.         another term is added to the group illustrating how you  can
  226.         add to a set.  All five are combined in the set named Mixed,
  227.         then  Mixed is subtracted from the entire set of  values  to
  228.         form the set of ingredients that are not used in any of  the
  229.         deserts.   Each ingredient is then checked to see if  it  is
  230.         "in"  the set of unused ingredients, and printed out  if  it
  231.         is.   Note  that "in" is another reserved  word  in  Pascal.
  232.         Running the program will reveal a list of unused elements.
  233.  
  234.              In this example, better programming practice would have
  235.         dictated defining a new variable, possibly called  Remaining
  236.         for the ingredients unused in line 32.  It was desirable  to
  237.         illustrate  that  Mixed could be assigned a value  based  on
  238.         subtracting itself from the entire set, so the poor variable
  239.         name was used.
  240.  
  241.              When you compile and run this program you will see that
  242.         this example results in some nonsense results but  hopefully
  243.         it  led your thinking toward the fact that sets can be  used
  244.         for  inventory control, possibly a parts allocation  scheme,
  245.         or some other useful system.
  246.  
  247.                             SEARCHING WITH SETS
  248.  
  249.              The  Pascal  program FINDCHRS is more useful  than  the
  250.         last  one.  In it we start with a short sentence and  search
  251.         it for all lower case alphabetic letters and write a list of
  252.         those  used.  Since we are using a portion of  the  complete
  253.         range  of  char, we do not need to define  a  scalar  before
  254.  
  255.  
  256.                                  Page 49
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.                 CHAPTER 8 - Scalars, subranges, and sets.
  267.  
  268.  
  269.         defining  the  set, we can define the set  using  the  range
  270.         'a'..'z'.   The  set Data_Set is assigned the  value  of  no
  271.         elements  in  the first statement of the  program,  and  the
  272.         print  string,  named Print_Group, is set to  blank  in  the
  273.         next.   The  variable Storage is assigned  the  sentence  to
  274.         search, and the search loop is begun.  Each time through the
  275.         loop,  one  of  the characters is  checked.   It  is  either
  276.         declared  as a non-lower-case character, as a repeat of  one
  277.         already  found,  or as a new character to be  added  to  the
  278.         list.
  279.  
  280.              You  are left to decipher the details of  the  program,
  281.         which should be no problem since there is nothing new  here.
  282.         Run  the  program and observe how the list  grows  with  new
  283.         letters as the sentence is scanned.
  284.  
  285.                            PROGRAMMING EXERCISE
  286.  
  287.         1. Modify FINDCHRS to search for upper-case letters.
  288.  
  289.  
  290.  
  291.  
  292.  
  293.  
  294.  
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.                                  Page 50
  323.